package com.banyou.banyou.util;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;


import com.banyou.banyou.MainApplication;

import static android.content.Context.ACTIVITY_SERVICE;
import static android.content.Context.TELEPHONY_SERVICE;

/**
 * Created by LX on 2017/4/7.
 * 获取手机中的一些状态
 */

public class PhoneState {
    private static AudioManager mAudioManager;
    public static int sLastSignalType = -1;

    static {
        mAudioManager = (AudioManager) MainApplication.getContext().getSystemService(Context.AUDIO_SERVICE);
    }

    /**
     * 获取手机信号类型
     *
     * @return
     */
    public static String getSignalType() {
        String strNetworkType = "";
        ConnectivityManager connManager = (ConnectivityManager) MainApplication.getContext().
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
        if (activeNetInfo != null && activeNetInfo.isConnected()) {
            if (activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                strNetworkType = "Wi-Fi";
            } else if (activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                String subtypeName = activeNetInfo.getSubtypeName();
                Log.d("flag", "___________subtypeName:" + subtypeName);
                int subtype = activeNetInfo.getSubtype();
                Log.d("flag", "___________subtype:" + subtype);
                switch (subtype) {
                    //如果是2g类型
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN:
                    case TelephonyManager.NETWORK_TYPE_GSM:
                        strNetworkType = "2G";
                        break;
                    //如果是3g类型
                    case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信3g
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
                    case TelephonyManager.NETWORK_TYPE_EHRPD:
                    case TelephonyManager.NETWORK_TYPE_HSPAP:
                    case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
                        strNetworkType = "3G";
                        break;
                    //如果是4g类型
                    case TelephonyManager.NETWORK_TYPE_LTE:
                        strNetworkType = "4G";
                        break;
                    default:
                        //中国移动 联通 电信 三种3G制式
                        if (subtypeName.equalsIgnoreCase("TD-SCDMA") ||
                                subtypeName.equalsIgnoreCase("WCDMA") ||
                                subtypeName.equalsIgnoreCase("CDMA2000") ||
                                subtypeName.equalsIgnoreCase("DS_HSDPA") ||
                                subtypeName.equalsIgnoreCase("TDS_HSDPA")) {
                            strNetworkType = "3G";
                        } else if (subtypeName.equalsIgnoreCase("TD-LTE") ||
                                subtypeName.equalsIgnoreCase("FDD-LTE")
                                || subtypeName.toUpperCase().contains("LTE")) {
                            strNetworkType = "4G";
                        }
                }
            }
        } else {
            strNetworkType = "断网";
        }
        return strNetworkType;
    }

    /**
     * 获取手机网络类型（int）
     * @param type
     * @return
     */
    public static int typeStr2Int(String type) {
        int singleType = -1;
        switch (type) {
            case "断网":
                singleType = 0;
                break;
            case "Wi-Fi":
                singleType = 1;
                break;
            case "2G":
                singleType = 2;
                break;
            case "3G":
                singleType = 3;
                break;
            case "4G":
                singleType = 4;
                break;
        }
        return singleType;
    }

    /**
     * 获取音乐音量
     *
     * @return
     */
    public static String getMusicVolume() {
        return mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) + "";
    }

    /**
     * 获取剩余内存
     *
     * @return
     */
    public static String getMemoryLeft() {
        ActivityManager sManager;
        sManager = (ActivityManager) MainApplication.getContext().getSystemService(ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
        sManager.getMemoryInfo(outInfo);
        return outInfo.availMem / 1024 / 1024 + "M";
    }

    /**
     * 获取手机型号
     *
     * @return
     */
    public static String getPhoneModel() {
        return Build.MODEL;
    }

    /**
     * 获取手机品牌
     *
     * @return
     */
    public static String getPhoneBrand() {
        return  Build.BRAND;
    }

    /**
     * 获取Android版本
     *
     * @return
     */
    public static String getAndroidVersion() {
        return Build.VERSION.SDK_INT + "";
    }

    /**
     * 获取App版本
     *
     * @return
     */
    public static String getAppVersion() {
        // 获取packagemanager的实例
        PackageManager packageManager = MainApplication.getContext().getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(MainApplication.getContext().getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = packInfo.versionName;
        return version;
    }

    /**
     * 获取IMEI
     *
     * @return
     */
    public static String getIMEI() {
        String deviceId;
        try {
            deviceId = ((TelephonyManager) MainApplication.getContext().getSystemService(TELEPHONY_SERVICE)).getDeviceId();
        }catch (Exception e){
            return "为授予查看IMEI权限";
        }

        return deviceId;
    }
}
